package com.puyuba.puyuba.view.fragment;

import android.view.View;
import android.widget.LinearLayout;

import com.flyco.tablayout.SlidingTabLayout;
import com.puyuba.puyuba.R;
import com.pyb.java.basemodule.BaseFragment;
import com.puyuba.puyuba.network.BaseApis;
import com.puyuba.puyuba.view.viewpager.WrapViewPager;
import com.pyb.java.commonutil.utils.StatusBarUtil;

import java.util.ArrayList;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

/**
 * 学习报名Fragment
 */
public class LearnRegistrationFragment extends BaseFragment {
    private SlidingTabLayout mTabLayout;
    private WrapViewPager mViewPager;
    private String[] mTitles = {"职业证","学历证","职业技能" ,"联合办学"};
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_learn;
    }

    @Override
    public void initView() {
        View viewStatusBar = (View) getActivity().findViewById(R.id.view_status_bar2);
        int screenHeight = StatusBarUtil.getStatusBarHeight(getContext());
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) viewStatusBar.getLayoutParams();
        layoutParams.height = screenHeight;
        viewStatusBar.setLayoutParams(layoutParams);
        mTabLayout = (SlidingTabLayout) getActivity().findViewById(R.id.tl_2);
        mViewPager = (WrapViewPager) getActivity().findViewById(R.id.viewpager_learn);
        initFragments();
        initTab();
    }

    private void initFragments() {
        mFragments.clear();
        mFragments.add(WebviewFragment.getInstance(BaseApis.PROFESSIONUrl));
//        mFragments.add(WebviewFragment.getInstance("https://t.puyuba.com/app/find/taocan"));   // 支付测试
        mFragments.add(WebviewFragment.getInstance(BaseApis.EDUCATIONUrl));
        mFragments.add(WebviewFragment.getInstance(BaseApis.PROFESSION_SKILL_Url));
        mFragments.add(WebviewFragment.getInstance(BaseApis.COMBINEDUrl));

    }

    private void initTab() {
        mViewPager.setAdapter(new MyPagerAdapter(getChildFragmentManager()));
        mViewPager.setOffscreenPageLimit(3);
        mTabLayout.setViewPager(mViewPager);
        mTabLayout.setOnTabSelectListener(new com.flyco.tablayout.listener.OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mViewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mTabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mViewPager.setCurrentItem(0);
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }


        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }
}
